package com.explorati.LeetCode091.numDecodings;

/**
 * @ Author : Weijian_Wang
 * @ Date : Created in 22:56 2020/10/22 0022
 * @ Description ：正向动态规划
 */
public class Solution3 {
    public int numDecodings(String s) {
        int len = s.length();
        int[] dp = new int[len + 1];
        dp[0] = 1;

        if ( s.charAt(0) != '0') {
            dp[1] = 1;
        }
        for (int i = 2; i <= len; i++) {
            int ans1 = 0;
            if (s.charAt(i - 1) != '0') {
                ans1 = dp[i - 1];
            }
            int ans2 = 0;
            if (s.charAt(i - 2) != '0') {
                int ten = (s.charAt(i - 2) - '0') * 10;
                int one = s.charAt(i - 1) - '0';
                if (ten + one <= 26) {
                    ans2 = dp[i - 2];
                }
            }
            dp[i] = ans1 + ans2;

        }
        return dp[len];
    }
}
